
#### ssh免密登录 初始化 脚本

serverIps='192.168.10.45 192.168.10.46'
#serverIps='192.168.41.204 192.168.41.205 192.168.41.206 192.168.41.207 192.168.41.208 192.168.41.210'
server_username=wasadmin
server_password=wasadmin

########### 配置项结束 ############

############## 模拟交互工具expect，用于需要控制台输入参数的场景
expectFile=`which expect 2>/dev/null`
if [ ! -f "$expectFile" ]; then
 echo "[WARN] tool[expect] not installed"
fi

#####确认安装了ssh
sshFile=`which ssh 2>/dev/null`
if [ ! -f "$sshFile" ]; then
 echo "[ERROR] [ssh] not installed"
 exit
fi

if [ ! -f "$HOME/.ssh/id_rsa.pub" ]
then
    expect <<EOF
        spawn ssh-keygen -t rsa
        expect  "Enter file" {send "\r"}
        expect  {
            "Overwrite" {send "n\r";exp_continue}
            "Enter passphrase" {send "\r"}
        }
        expect "Enter same passphrase again" {send "\r"}
        expect eof
EOF
#  expect eof  防止文件没生成，脚本就中断了
fi

for var in $serverIps
do
    expect <<EOF
        spawn ssh-copy-id ${server_username}@$var
        expect {
            "yes/no" { send "yes\r";exp_continue }
            "password" { send "$server_password\r" }
          }
        expect "password" {send "$server_password\r"}
EOF
done



